//
//  WD_HomeViewController.m
//  BUDING
//
//  Created by HanWoonYong on 16/3/31.
//  Copyright © 2016年 HanWoonYong. All rights reserved.
//

#import "WD_HomeViewController.h"
#import "WD_GroupViewController.h"
#import "WD_PartViewController.h"
#import "WD_RecomViewController.h"
#import "AppDelegate.h"
#import "WD_HomeDetailViewController.h"
#import "WD_MyPlayer.h"
@interface WD_HomeViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UISegmentedControl *seg;
@property (nonatomic, strong) UIView *segView;
@property (nonatomic, strong) WD_GroupViewController *groupVC;
@property (nonatomic, strong) WD_PartViewController *partVC;
@property (nonatomic, strong) WD_RecomViewController *recomVC;
@property (nonatomic, strong) WD_MyPlayer *player;
@property (nonatomic, copy) NSString *idString;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, assign) NSInteger section;
@property (nonatomic, copy) NSString *url;
@end

@implementation WD_HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = [NSMutableArray arrayWithCapacity:0];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(openOrCloseLeft)];
    [self setUpSeg];
    [self setUpView];
    
}

- (void)setUpSeg {
    _seg = [[UISegmentedControl alloc] initWithItems:@[@"新番", @"推荐", @"分区"]];
    _seg.frame = CGRectMake(0, 0, WIDTH / 2, self.navigationController.view.bounds.size.height / 20);
    _seg.selectedSegmentIndex = 1;
    _seg.center = self.navigationController.navigationBar.center;
    _seg.tintColor = [UIColor clearColor];
    NSDictionary *seletedTextAttribues = @{NSFontAttributeName:[UIFont systemFontOfSize:20]};
NSForegroundColorAttributeName:[UIColor whiteColor];
    [_seg setTitleTextAttributes:seletedTextAttribues forState:UIControlStateSelected];
    NSDictionary *unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20], NSForegroundColorAttributeName:[UIColor blackColor]};
    [_seg setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    
    [_seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    self.segView = [[UIView alloc] initWithFrame:CGRectMake(0, self.seg.bounds.size.height, self.seg.bounds.size.width / 3, 5)];
    self.segView.backgroundColor = [UIColor orangeColor];
    [_seg addSubview:self.segView];
    
    [self.navigationController.view addSubview:_seg];
    
    
}

- (void)setUpView {
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView.contentSize = CGSizeMake(WIDTH * 3, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.groupVC = [[WD_GroupViewController alloc] init];
    [self addChildViewController:self.groupVC];
    self.groupVC.view.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    [self.scrollView addSubview:self.groupVC.view];
    self.recomVC = [[WD_RecomViewController alloc] init];
    [self addChildViewController:self.recomVC];
    self.recomVC.view.frame = CGRectMake(WIDTH, 0, WIDTH, HEIGHT);
    __weak WD_HomeViewController *myVC = self;
    
    self.recomVC.idBlock = ^(NSString *fuck){
        myVC.idString = fuck;
        //        NSLog(@"*****      %@      *****", myVC.idString);
        [myVC.array addObject:myVC.idString];
    };
    self.recomVC.cellButBlock = ^{
        
        [myVC cellButBlockAction];
    };
    self.recomVC.cellBlock = ^{
        
        [myVC cellBlockAction];
    };
    self.recomVC.numBlock = ^(NSInteger num){
        myVC.num = num;
    };
    self.recomVC.sectionBlock = ^(NSInteger section){
        myVC.section = section;
    };
    self.recomVC.dontPlayBlock = ^{
        [myVC recomVCdontPlay];
    };
    [self.scrollView addSubview:self.recomVC.view];
    
    self.partVC = [[WD_PartViewController alloc] init];
    [self addChildViewController:self.partVC];
    self.partVC.view.frame = CGRectMake(WIDTH * 2, 0, WIDTH, 300);
    [self.scrollView addSubview:self.partVC.view];
    [self.view addSubview:self.scrollView];
    
}

#pragma mark - SearchAction

- (void)rightAction:(UIBarButtonItem *)sender {
    
}

#pragma mark - leftAction

- (void)openOrCloseLeft {
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (tempAppDelegate.leftSlideVC.closed) {
        [tempAppDelegate.leftSlideVC openLeftView];
    } else {
        [tempAppDelegate.leftSlideVC closeLeftView];
    }
    
    
}

#pragma mark - ScrollViewDidScroll

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    self.seg.selectedSegmentIndex = scrollView.contentOffset.x / self.scrollView.bounds.size.width;
    if (scrollView.contentOffset.x == 0) {
        self.segView.frame = CGRectMake(0, self.seg.bounds.size.height + 1, self.seg.bounds.size.width / 3, 3);
    } else if (scrollView.contentOffset.x == self.view.bounds.size.width) {
        self.segView.frame = CGRectMake(self.segView.bounds.size.width, self.seg.bounds.size.height + 1, self.seg.bounds.size.width / 3, 3);
    } else if (scrollView.contentOffset.x == self.view.bounds.size.width * 2) {
        self.segView.frame = CGRectMake(self.segView.bounds.size.width * 2, self.seg.bounds.size.height + 1, self.seg.bounds.size.width / 3, 3);
    }
    
}


- (void)segAction:(UISegmentedControl *)seg {
    
    NSLog(@"%ld", seg.selectedSegmentIndex);
    self.scrollView.contentOffset = CGPointMake(WIDTH * self.seg.selectedSegmentIndex, 0);
    if (seg.selectedSegmentIndex == 0) {
        self.segView.frame = CGRectMake(self.segView.bounds.size.width * 2, self.seg.bounds.size.height + 1, self.seg.bounds.size.width / 3, 3);
        [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:100 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.segView.frame = CGRectMake(0, self.seg.bounds.size.height + 1, self.seg.bounds.size.width / 3, 3);
            
        } completion:^(BOOL finished) {
            
            
        }];
    } else if (seg.selectedSegmentIndex == 1) {
        
        self.segView.frame = CGRectMake(0, self.seg.bounds.size.height + 1, self.seg.bounds.size.width / 3, 3);
        [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:100 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.segView.frame = CGRectMake(self.segView.bounds.size.width, self.seg.bounds.size.height + 1, self.seg.bounds.size.width / 3, 3);
            
        } completion:^(BOOL finished) {
            
        }];
    } else if (seg.selectedSegmentIndex == 2) {
        
        self.segView.frame = CGRectMake(0, self.seg.bounds.size.height + 1, self.seg.bounds.size.width / 3, 3);
        [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:100 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.segView.frame = CGRectMake(self.segView.bounds.size.width * 2, self.seg.bounds.size.height + 1, self.seg.bounds.size.width / 3, 3);
            
        } completion:^(BOOL finished) {
            
            
        }];
    }
}

- (void)cellButBlockAction {
    
    //    [self getInfo];
}

- (void)cellBlockAction {
    self.seg.hidden = YES;
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.leftSlideVC closeLeftView];
    WD_HomeDetailViewController *homeDetailVC = [[WD_HomeDetailViewController alloc] init];
    homeDetailVC.idStr = self.idString;
    
    homeDetailVC.backRootBlock = ^(NSString *url) {
        
        if (!self.player) {
            [self creatPlayer];
        } else {
            [self.player removeFromSuperview];
            [self creatPlayer];
        }
    };
    NSLog(@"++++++ %ld +++++++", self.section);
    [self.navigationController pushViewController:homeDetailVC animated:YES];
    
}

- (void)creatPlayer {
    self.player = [[WD_MyPlayer alloc] initWithMyPlayFrame:CGRectMake(100, 100, 200, 150)];
    UIButton *vainsh = [UIButton buttonWithType:UIButtonTypeSystem];
    vainsh.frame = CGRectMake(0, 0, 20, 20);
    [vainsh setImage:[[UIImage imageNamed:@"Image-8"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [vainsh addTarget:self action:@selector(vainshAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.player addSubview:vainsh];
    self.player.pan.enabled = NO;
    self.player.tap.enabled = NO;
    //        self.player.url = url;
    self.player.maskView.hidden = YES;
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [self.player addGestureRecognizer:pinch];
    [self.player addGestureRecognizer:tap];
    [self.player addGestureRecognizer:pan];
    [self.player.player play];
    [self.view addSubview:self.player];
    
}



#pragma mark - 长按事件

#pragma mark - 推拽事件
- (void)panAction:(UIPanGestureRecognizer *)pan{
    
    UIView *panView = (UIView *)pan.view;
    CGPoint p = [pan translationInView:panView];
    panView.transform = CGAffineTransformTranslate(panView.transform, p.x, p.y);
    [pan setTranslation:CGPointZero inView:panView];
    
    
}
#pragma mark - 轻拍
- (void)tapAction:(UITapGestureRecognizer *)tap {
    UIView *tapView = (UIView *)tap.view;
    NSLog(@"%@", tapView);
    
}
#pragma mark - 捏合
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {
    UIView *pinchView = (UIView *)pinch.view;
    
    self.player.transform = CGAffineTransformScale(pinchView.transform, pinch.scale, pinch.scale);
    pinch.scale = 1;
    
}

- (void)vainshAction:(UIButton *)sender {
    [UIView animateWithDuration:1 animations:^{
        self.player.alpha--;
    }];
    
    [self.player.player pause];
}

- (void)recomVCdontPlay {
    [self.player.player pause];
}

- (void)viewDidDisappear:(BOOL)animated {
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.leftSlideVC setPanEnabled:YES];
    [self.player removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
